<?php
/** [附件表逻辑层模型]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-15 10:50:57
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-03-01 00:06:28
 */
class UploadLogic extends Model{

	protected $tableName = 'upload';


	/**
	 * [update_uoload_attachment 更新数据]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function update_uoload_attachment($aid)
	{
		//p($_SESSION['keditor']);
		// 必须有上传附件
		if(isset($_SESSION['keditor']))
		{
			$uploadModel = D('Upload');
			// 遍历session 更新关联
			foreach($_SESSION['keditor'] as $v)
			{
				$uploadModel->save(array('article_aid'=>$aid,'id'=>$v));
			}

			
		}
		// 清空
		unset($_SESSION['keditor']);
	}



	/**
	 * [delete_upload_by_article_aid 删除附件通过关联外键article_aid]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function delete_upload_by_article_aid($aid)
	{
		$data = $this->where(array('article_aid'=>$aid))->select();
		if(!$data)
			return;
		foreach($data as $v)
		{
			$fullPath = $v['path'].'/'.$v['name'].'.'.$v['ext'];
			is_file($fullPath) && unlink($fullPath);
		}
		$this->where(array('article_aid'=>$aid))->delete();
	}

	/**
	 * [get_all 读取附件]
	 * @return [type] [description]
	 */
	public function get_all()
	{
		$uploadViewModel = D('UploadView');
		$count = $uploadViewModel->count();
		import('ORG.Util.Page');// 导入分页类
		$page  = new Page($count,$count);
		$limit = $page->firstRow.','.$page->listRows;
		$data = $uploadViewModel->find_limit($order,$limit);
		
		if(!$data) return $data;
		foreach($data as $k=>$v)
		{
			// 大小
			$data[$k]['size']    = get_size($v['size']);
			//时间
			$data[$k]['time']    = $v['addtime'];
			$data[$k]['addtime'] = date('Y-m-d',$v['addtime']);

			// 图片
			$file = $v['path'].'/'.$v['name'].'.'.$v['ext'];
			$data[$k]['ispic']   = $ispic =getimagesize($file);
			$data[$k]['img']     = $ispic?__ROOT__.'/'.$file:__ROOT__.'/Data/Public/images/default.gif';

			$data[$k]['state']  = $v['article_aid']?'有效':'无效';

		}

		// 分页样式更改
		$page->setConfig('theme','<span>%totalRow% %header% %nowPage%/%totalPage% 页 </span>%upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');

		$data['page'] = $page->show();
		//echo $uploadViewModel->_sql();
		return $data;
	}

	/**
	 * [del_attachment 删除附件]
	 * @return [type] [description]
	 */
	public function del_attachment()
	{
		$id   = I('get.id');
		if(!$this->del_attachment_by_id($id))
			return false;

		return true;
	}
	public function del_attachment_by_id($id)
	{
		$data = $this->find($id);
	
		if($data['article_aid'])
		{
			$this->error ='附件正在使用中';
			return false;
		}

		$fullPath = $data['path'].'/'.$data['name'].'.'.$data['ext'];
		is_file($fullPath) && unlink($fullPath);

		$this->delete($id);
		return true;
	}




	/**
	 * [del_no_use_attachment 删除无效附件]
	 * @return [type] [description]
	 */
	public function del_no_use_attachment()
	{
		// 找到article_aid等于0的所有附件
		$where['article_aid'] = 0;
		$data = $this->where($where)->select();
		if(!$data)
		{
			$this->error = '没有无效附件';
			return false;
		}
		// 遍历删除
		foreach($data as $v)
		{
			$this->del_attachment_by_id($v);
		}

		return true;
	}

}

 ?>